/**
 * 得到百分比(保留两位)
 */
export function getPercentage(numb: number | string) {
  return `${Math.round(Number(numb) * 10000) / 100}%`;
}

/**
 * 整理数字 (四舍五入保留n为小数)
 */
export function organizeDigital(numb: number, decimalPlaces: number) {
  const multiplier = 10 ** decimalPlaces;
  return Math.round(numb * multiplier) / multiplier;
}

/**
 * 根据对象的某两个key相乘
 * @param val1 - 乘数的key
 * @param val2 - 被乘数的key
 * @param pos - 保留几位小数
 */
export function getTotal(val1: number | string, val2: number | string, pos: number) {
  const newVal1 = !isNaN(Number(val1)) ? Number(val1) : 0;
  const newVal2 = !isNaN(Number(val2)) ? Number(val2) : 0;
  return organizeDigital(newVal1 * newVal2, pos);
}

/**
 * 根据对象数组的某个key得到合计
 * @param list - 对象数组
 * @param key - 需要计算的key
 * @param pos - 保留几位小数
 */
export function getTotalByObjArrKey<T extends Record<string, unknown>, K extends keyof T>(list: T[], key: K, pos: number) {
  const total = list.reduce((_total, curr) => _total + (!isNaN(Number(curr[key])) ? Number(curr[key]) : 0), 0);
  return organizeDigital(total, pos);
}

/**
 * 根据对象数组的某两个key相乘后在得到合计
 * @param list - 对象数组
 * @param key1 - 乘数的key
 * @param key2 - 被乘数的key
 * @param pos - 保留几位小数
 */
export function getMultiplyTotalByObjArrKey<T extends Record<string, unknown>, K extends keyof T>(list: T[], key1: K, key2: K, pos: number) {
  const total = list.reduce((_total, curr) => {
    const val1 = !isNaN(Number(curr[key1])) ? Number(curr[key1]) : 0;
    const val2 = !isNaN(Number(curr[key2])) ? Number(curr[key2]) : 0;
    return _total + val1 * val2;
  }, 0);
  return organizeDigital(total, pos);
}

/** 得到最大公约数 */
export function getGCD(_a: number, _b: number): number {
  let a = _a;
  let b = _b;
  while (b !== 0) {
    const temp = b;
    b = a % b;
    a = temp;
  }
  return a;
}

/** 四舍五入保留指定小数位数 */
export const customRound = (number: number, decimalPlaces = 2) => {
  const factor = 10 ** decimalPlaces;
  const tempNumber = number * factor;
  const roundedTempNumber = Math.round(tempNumber);
  let result = roundedTempNumber / factor;
  if (Math.abs(result - number) === 0.5 * 0.1 ** decimalPlaces) {
    result = result < number ? result + 0.1 * 0.1 ** decimalPlaces : result - 0.1 * 0.1 ** decimalPlaces;
  }
  return result;
};

/** Number格式化为货币 */
export function formatNumberWithMoney(number: number | string | null | undefined, decimalPlaces = 2) {
  if (isNaN(Number(number))) {
    return number;
  }

  const _number = customRound(Number(number), decimalPlaces);

  let [integer, decimal = ''] = `${_number}`.split('.');
  integer = new Intl.NumberFormat().format(Number(integer));
  if (decimal === '') {
    return integer;
  }
  decimal = decimal.split('').reverse().join('');
  decimal = new Intl.NumberFormat().format(Number(decimal));
  decimal = decimal.split('').reverse().join('');
  return `${integer}.${decimal}`;
}

/** 两数相除结果为百分比形式 */
export function convertToPercentage(_num1: number | null, _num2: number | null) {
  const percentage = divideNumbers(_num1, _num2) * 100;
  if (isNaN(percentage)) {
    return '0%';
  } else {
    return percentage === 0 ? '0%' : `${customRound(percentage)}%`;
  }
}

/** 除法 */
export function divideNumbers(dividend: number | string | null | undefined, divisor: number | string | null | undefined): number {
  const num1 = Number(dividend);
  const num2 = Number(divisor);

  if (isNaN(num1) || isNaN(num2)) {
    return 0;
  }

  if (num2 === 0) {
    return 0;
  }

  return num1 / num2;
}
